package edu.hwadee.action;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ModelDriven;

import edu.hwadee.entity.Clientbuy;
import edu.hwadee.entity.Materialsell;

public class ClientbuyAction extends BasicAction implements ModelDriven<Clientbuy> {

	private Clientbuy clientbuy;
	private int clientmaterialid;
	private int buynumber;
	private float price;
	private int buycompanyid;
	private int sellcompanyid;
	private int dostaffid;
	private short state;

	@Override
	public Clientbuy getModel() {
		if (clientbuy == null)
			clientbuy = new Clientbuy();
		return clientbuy;
	}

	public String addCustomerClientbuy() {
		clientbuy.setSellDate(new Date());
		clientbuy.setEarn(getPrice());
		clientbuy.setReceivedMoney((float) 0);
		clientbuy.setPaymentState((short) 0);
		clientbuy.setTaxFee(getMaterialServiceImpl().searchById(clientmaterialid).getTaxRate() * getPrice() / 100);
		clientbuy.setProfit(clientbuy.getEarn()
				- getMaterialServiceImpl().searchById(clientmaterialid).getPrice() * buynumber - clientbuy.getTaxFee());
		Integer id = getClientbuyServiceImpl().save(clientbuy);
		Materialsell materialsell = new Materialsell();
		materialsell.setMaterial(getMaterialServiceImpl().searchById(clientmaterialid));
		materialsell.setClientbuy(getClientbuyServiceImpl().searchById(id));
		materialsell.setEarn(price);
		materialsell.setQuanity((float) buynumber);
		getMaterialsellServiceImpl().save(materialsell);
		return "success";
	}

	public String searchClientbuy() {
		Clientbuy client = getClientbuyServiceImpl().searchById(clientbuy.getClientBuyId());
		ServletActionContext.getRequest().getSession().setAttribute("clientbuy", client);
		ServletActionContext.getRequest().getSession().setAttribute("searchclientbuy", true);
		setClientbuy(null);
		return "success";
	}

	public String showAllClientbuy() {
		List<Clientbuy> list = getClientbuyServiceImpl().showAll();
		ServletActionContext.getRequest().getSession().setAttribute("clientbuy", list);
		return "success";
	}

	@SuppressWarnings("unchecked")
	public String deleteClientbuy() {
		System.out.println(clientbuy.getClientBuyId());
		Clientbuy clb = getClientbuyServiceImpl().searchById(clientbuy.getClientBuyId());
		Set<Materialsell> set = clb.getMaterialsells();
		for (Materialsell materialsell : set) {
			getMaterialsellServiceImpl().delete(materialsell);
		}
		getClientbuyServiceImpl().delete(clb);
		return "success";
	}

	public String updateClientbuy() {
		System.out.println(state);
		clientbuy.setCompanyByBuycompany(getCompanyServiceImpl().searchById(buycompanyid));
		clientbuy.setCompanyBySaleCompany(getCompanyServiceImpl().searchById(sellcompanyid));
		clientbuy.setStaff(getStaffServiceImpl().searchById(dostaffid));
		clientbuy.setPaymentState(state);
		getClientbuyServiceImpl().update(clientbuy);
		return "success";
	}

	public Clientbuy getClientbuy() {
		return clientbuy;
	}

	public void setClientbuy(Clientbuy clientbuy) {
		this.clientbuy = clientbuy;
	}

	public int getClientmaterialid() {
		return clientmaterialid;
	}

	public void setClientmaterialid(int clientmaterialid) {
		this.clientmaterialid = clientmaterialid;
	}

	public int getBuynumber() {
		return buynumber;
	}

	public void setBuynumber(int buynumber) {
		this.buynumber = buynumber;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public int getBuycompanyid() {
		return buycompanyid;
	}

	public void setBuycompanyid(int buycompanyid) {
		this.buycompanyid = buycompanyid;
	}

	public int getSellcompanyid() {
		return sellcompanyid;
	}

	public void setSellcompanyid(int sellcompanyid) {
		this.sellcompanyid = sellcompanyid;
	}

	public int getDostaffid() {
		return dostaffid;
	}

	public void setDostaffid(int dostaffid) {
		this.dostaffid = dostaffid;
	}

	public short getState() {
		return state;
	}

	public void setState(short state) {
		this.state = state;
	}

}
